package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import config.AlipayConfig;

/**
 * 用来处理支付的servlet 
 *  需要生成一个支付的二维码 -->要发送 请求到  支付宝的dev(开发)环境
 *  
 *  请求长什么样子?
 *  发送到哪里去?
 *  数据如何接受??
 *  
 *  以上请参考 官网demo中的 alipay.trade.page.pay.jsp
 *  
 */
@WebServlet("/AlipaymentServlet")
public class AlipaymentServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out= response.getWriter();
		
		//获得初始化的AlipayClient
		AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl
																						, AlipayConfig.app_id
																						, AlipayConfig.merchant_private_key
																						, "json"
																						, AlipayConfig.charset
																						, AlipayConfig.alipay_public_key
																						, AlipayConfig.sign_type);
		
		//设置请求参数
		AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
		
		//在新版的文档接口中,没有这2行代码
		//支付成功之后,回调的同步/异步通知页面
		alipayRequest.setReturnUrl(AlipayConfig.return_url);
		alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
		
		//商户订单号，商户网站订单系统中唯一订单号，必填
		//根据当前时间,生成唯一的订单号
		Date now =new Date();
		SimpleDateFormat sdf =new SimpleDateFormat("yyyyMMddhhmmssms");
		String out_trade_no =  sdf.format(now);  //2020323155221267
		
		//付款金额，必填
		String total_amount = request.getParameter("WIDtotal_amount"); //前端jsp的参数
		//订单名称，必填
		String subject = "这是我的测试订单";
		//商品描述，可空
		String body = "这是我的商品描述";
		
		//请求参数 key -的值是固定的 value是自定义的
		alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"," 
				+ "\"total_amount\":\""+ total_amount +"\"," 
				+ "\"subject\":\""+ subject +"\"," 
				+ "\"body\":\""+ body +"\"," 
				+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
		
		//若想给BizContent增加其他可选请求参数，以增加自定义超时时间参数timeout_express来举例说明
		//alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"," 
		//		+ "\"total_amount\":\""+ total_amount +"\"," 
		//		+ "\"subject\":\""+ subject +"\"," 
		//		+ "\"body\":\""+ body +"\"," 
		//		+ "\"timeout_express\":\"10m\"," 
		//		+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
		//请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节
		
		//请求
		String result= "";
		try {
			//AlipayTradePagePayResponse response = alipayClient.pageExecute(request); 官网接口代码
			result = alipayClient.pageExecute(alipayRequest).getBody(); //pay.jsp
			System.out.println("--result:  "+result);
			//输出
			out.println(result); //jsp 输出 一个二维码
		} catch (AlipayApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
